package com.ggboy.item_bank.service.imp;

import com.ggboy.item_bank.beans.paper1;
import com.ggboy.item_bank.dao.QstTypeDao;
import com.ggboy.item_bank.service.PaperService;
import com.ggboy.item_bank.service.SubjectService;
import com.ggboy.item_bank.utils.ArrayUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.ws.ServiceMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Service
public class PaperServiceImpl implements PaperService {

    @Resource
    private QstTypeDao qstTypeDao;


    //在总题表中找出符合类型的题，并将他们打乱
    public paper1 capacity1(String subject, Integer choiceCount, Integer tofCount, Integer designCount, Integer bigCount, Integer level, Integer teacherId) {
        //查询出所有符合该科目并且是该类型题和规定难度的所有该类型题的ID
        String[] choiceIdList =  qstTypeDao.selectQstByLevelAndSubjectAndType(subject,level,"选择题");
        String[] tofIdList = qstTypeDao.selectQstByLevelAndSubjectAndType(subject, level, "判断题");
        String[] designIdList = qstTypeDao.selectQstByLevelAndSubjectAndType(subject, level, "简答题");
        String[] bigIdList = qstTypeDao.selectQstByLevelAndSubjectAndType(subject, level, "应用题");

        Random random = new Random();
        int tempId1;
        //该循环用来打乱题目，达到随机去题的效果
        for (int i = 0;i <= choiceIdList.length - 1;i++){
            tempId1 = random.nextInt(choiceIdList.length);//返回0到题目数-1的随机整数数
            String temp = choiceIdList[i];//顺序获取的获取题目
            choiceIdList[i] = choiceIdList[tempId1];//获取随机下标的题目，赋值给当前题目
            choiceIdList[tempId1] = temp;//将顺序获取的题目，给到随机位置
        }
        //创建一个选择题数大小的数组
        String[] choiceId = new String[choiceCount];
        for (int i = 0;i <= choiceCount-1;i++){//在总题目数组中抽取题目赋值给你选择的题目数，如果总题目数不够会抛出异常
            choiceId[i] = choiceIdList[i];
        }
        String choice = ArrayUtils.toStrings(choiceId);//调用工具类把字符串数组转为一个字符串

        int tempId2;
        for (int i = 0;i <= tofIdList.length - 1;i++){//用于打乱题目数
            tempId2 = random.nextInt(tofIdList.length);
            String temp = tofIdList[i];
            tofIdList[i] = tofIdList[tempId2];
            tofIdList[tempId2] = temp;
        }

        String[] tofId = new String[tofCount];
        for (int i = 0;i <= tofCount-1;i++){//在总题目数组中抽取题目赋值给你选择的题目数
            tofId[i] = tofIdList[i];
        }
        String tof = ArrayUtils.toStrings(tofId);//调用工具类把字符串数组转为一个字符串

        int tempId3;
        for (int i = 0;i <= designIdList.length - 1;i++){
            tempId3 = random.nextInt(designIdList.length);
            String temp = designIdList[i];
            designIdList[i] = designIdList[tempId3];
            designIdList[tempId3] = temp;
        }

        String[] designId = new String[designCount];
        for (int i = 0;i <= designCount-1;i++){
            designId[i] = designIdList[i];
        }
        String design = ArrayUtils.toStrings(designId);

        int tempId4;
        for (int i = 0;i <= bigIdList.length - 1;i++){
            tempId4 = random.nextInt(bigIdList.length);
            String temp = bigIdList[i];
            bigIdList[i] = bigIdList[tempId4];
            bigIdList[tempId4] = temp;
        }

        String[] bigId = new String[bigCount];
        for (int i = 0;i <= bigCount-1;i++){
            bigId[i] = bigIdList[i];
        }
        String big = ArrayUtils.toStrings(bigId);

        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String format = simpleDateFormat.format(now);//获取当前时间
        //将科目信息，时间，当前做题用户的id,和随机出来符合要求的各题及题目难度信息封装成javaBean对象
//        Paper paper = new Paper(subject, format, teacherId, choice, tof, design, big, level.toString());
        paper1 paper1 = new paper1(subject, format, teacherId, choiceId, tofId, designId, bigId, level.toString());

        return paper1;//返回javaBean
    }

}
